package hellfire.platform.graphics;

import android.util.Log;
import android.view.MotionEvent;
import hellfire.platform.input.TouchListener;
import hellfire.system.R;
import hellfire.system.graphics.GLColor;
import hellfire.system.graphics.GLDrawable;
import hellfire.system.graphics.GLImage;
import hellfire.system.graphics.GLString;
import hellfire.system.graphics.GLStringList;
import hellfire.system.graphics.GLVec2;

public class GLCoolList extends GLStringList implements TouchListener
{
	private GLString title;
	private GLImage background;
	private GLVec2 pos, dim;
	
	private Integer selectedIdx = null;
	
	private GLColor selTextColor = GLColor.GREEN;
	private GLColor selTextBackground = new GLColor(1, 0.5f, 0, 0.5f);
	
	public GLCoolList(String title, GLVec2 pos, GLVec2 dim)
	{
		super(pos.sub(0, dim.y()/10), dim.sub(0, dim.y()/6), dim.y() / 10);
		this.pos = pos;
		this.dim = dim;
		this.background = new GLImage(R.drawable.list, pos, dim);
		this.background.setMaskColor(GLColor.RED);
		
		this.title = new GLString(R.drawable.basefont, title, this.pos.add(-dim.x() / 3, this.dim.y() / 1.2f), this.dim.y() / 10);
		this.title.setPos(new GLVec2(pos.x(), this.title.getPos().y()));
	}
	
	/**
	 * 
	 * @return index of selected string or null
	 */
	public Integer getSelectedIndex()
	{
		return selectedIdx;
	}

	public String getSelectedString()
	{
		if(selectedIdx == null) return null;
		return strings.get(selectedIdx).getString();
	}

	public GLString getCoreStringAtIndex(int index)
	{
		if(index < 0 || index >= strings.size()) return null;
		return strings.get(index);
	}
	
	@Override
	public void draw(float[] matrix) 
	{
		this.background.draw(matrix);
		this.title.draw(matrix);
		super.draw(matrix);
	}


	@Override
	public GLVec2 getPos() 
	{
		return this.background.getPos();
	}


	@Override
	public void setPos(GLVec2 pos) 
	{
		super.setPos(pos.sub(0, dim.y()/10));
		this.title.setPos(pos.add(0, this.dim.y() / 1.2f));
		this.background.setPos(pos);
	}

	public void deselect()
	{
		if(selectedIdx != null)
		{
			strings.get(selectedIdx).setBackgroundColor(textBackground);
			strings.get(selectedIdx).setTextColor(textColor);
			selectedIdx = null;
		}
	}
	
	public void selectIndex(Integer idx)
	{
		if(idx == null || idx < 0 || idx >= strings.size()) return;
		deselect();
		selectedIdx = idx;
		strings.get(selectedIdx).setBackgroundColor(selTextBackground);
		strings.get(selectedIdx).setTextColor(selTextColor);
	}
	

	@Override
	public void onTouchEvent(MotionEvent e, GLVec2 pos) 
	{
		if(e.getAction() != MotionEvent.ACTION_UP) return;
		float yStart = this.pos.y() + dim.y() * 11f/15f;
		float yEnd = this.pos.y() - this.dim.y();
		float selY = pos.y();
		
		
		float idxY = (selY - yStart) / (yEnd - yStart);
		if(idxY < 0) return;
		int idx = (int) (idxY * getNumVisibleElements());
		
		selectIndex(idx);
	}


	@Override
	public boolean testPosition(GLVec2 pos) 
	{
		return background.checkBounds(pos);
	}
}
